require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIAllocationResult = {}
HelperFunc.SetUIEnv(UIAllocationResult)
local this = UIAllocationResult

this.linkColor = "#" .. G.dataTable["t_global"][tostring(185)]["f_string_value"]

function UIAllocationResult.Init(data, closeFunc)
    this.result_data = data
    local _name, _package = getUIName(PanelNames.UIAllocationResult)
    panelMgr:ShowUI(_name, _package, this.onCreate, Config.useLocalUI)
end

function UIAllocationResult.onCreate(obj) 
    if not this.hasInit then
        this.hasInit = true
        this.bgClose = obj:GetChild("BgClose")
        this.content = obj:GetChild("Content")
        this.content:SetVirtual()
        this.addEvent()
    end 
    this.initData()
end

function UIAllocationResult.initData() 
    this.content.itemRenderer = this.UpdateItemRender
    this.content.numItems = this.result_data and #this.result_data.hero_data or 0
end

function UIAllocationResult.UpdateResultData( data )
    this.result_data = data
    if this.content then
        this.content.numItems = this.result_data and #this.result_data.hero_data or 0
    end
end

function UIAllocationResult.addEvent() 
    this.bgClose.onClick:Add(this.OnClickBack)
end

function UIAllocationResult.removeEvent() 
    this.bgClose.onClick:Clear()
end

function UIAllocationResult.UpdateItemRender( index, obj )
 
    local item_info = this.result_data.hero_data[index+1]
    local hero_info = item_info.item_info

    this.showItemInfo( obj, hero_info)

    this.showEquip(obj, false, 1, item_info.before_equip1)
    this.showEquip(obj, false, 2, item_info.before_equip2)
    this.showEquip(obj, false, 3, item_info.before_equip3)
    this.showEquip(obj, false, 4, item_info.before_equip4)

    this.showEquip(obj, true, 1, item_info.after_equip1)
    this.showEquip(obj, true, 2, item_info.after_equip2)
    this.showEquip(obj, true, 3, item_info.after_equip3)
    this.showEquip(obj, true, 4, item_info.after_equip4)
end

function UIAllocationResult.showEquip(obj, is_after, pos_index, equip_info) 
    local icon_str = "icon"
    local quality_str = "quality"
    local frame_str = "frame"
    local icon_suit_str = "iconSuit" 
    local num_str = "num" 

    if is_after then
        icon_str = "after_icon"
        quality_str = "after_quality"
        frame_str = "after_frame"
        icon_suit_str = "after_iconSuit"
        num_str = "after_num"
    end
    local img_Icon = obj:GetChild(icon_str..pos_index)
    local img_Quality = obj:GetChild(quality_str..pos_index)
    local img_Frame = obj:GetChild(frame_str..pos_index) 
    local txt_num = obj:GetChild(num_str..pos_index) 
    local icon_suit = obj:GetChild(icon_suit_str..pos_index)

    if equip_info ~= nil then
        if equip_info.icon ~= nil then
            img_Icon.visible = true
            img_Icon.url = "ui://ItemAtlas/"..equip_info.icon
        else
            img_Icon.visible = false
        end
        if equip_info.quality ~= nil then
            img_Quality.visible = true
            img_Frame.visible = true
            local quality = tonumber(equip_info.quality)
            img_Frame.url =  EnumConst.AtlasPath.ICONAtlas..Config.IconFrame[quality]
            img_Quality.url = EnumConst.AtlasPath.ICONAtlas..Config.IconQuality[quality]
        else
            img_Quality.visible = false
            img_Frame.visible = false
        end 
        local suit_index = 0
        if logicMgr.ItemManager.IsEquip(equip_info.itemModelId) then
            local cfg = dataTable.getDataTableByStringId("t_equip", equip_info.itemModelId)
            suit_index = cfg.f_EquipSuit  
        end
        if suit_index == 1 then
            icon_suit.visible = true
            icon_suit.url =  EnumConst.AtlasPath.ICONAtlas.."suit1"
        elseif suit_index == 2 then
            icon_suit.visible = true
            icon_suit.url =  EnumConst.AtlasPath.ICONAtlas.."suit2"
        else
            icon_suit.visible = false
        end
    else  
        img_Icon.visible = false
        txt_num.visible = false
        icon_suit.visible = false 

        img_Quality.visible = true
        img_Frame.visible = true 
        img_Frame.url =  EnumConst.AtlasPath.ICONAtlas.."icon_frame_00_01"
        img_Quality.url = EnumConst.AtlasPath.ICONAtlas.."icon_frame_00_00"
    end
    
end

function UIAllocationResult.showItemInfo( obj, item_info)

    local quality = obj:GetChild("quality")
    local qualityBG = obj:GetChild("qualityBG")
    local icon = obj:GetChild("icon") 
    local lv = obj:GetChild("level")
    local lvbg = obj:GetChild("levelbg")
    local occupation_icon = obj:GetChild("Occupation")  

    if item_info then  
        if item_info.star ~= nil then
            item_info.quality = logicMgr.HeroManager.starToQuality(item_info.star);
        end   
   
        if item_info.level ~= nil and item_info.level > 0 then  
            lv.visible = true
            lvbg.visible = true
            local level, lvType = logicMgr.HeroManager.getHeroShowLevelByPartnerId(item_info.partnerId)
            lv.text = string.TextColor(string.format(getLanguage("Level"), level), logicMgr.HeroManager.lvColors[lvType])
        else 
            lv.visible = false
            lvbg.visible = false 
        end 
 
        if item_info.star ~= nil and item_info.star > 0 then 
            local numstar = math.floor(item_info.star * 0.0001)
            if numstar > 5 then
                numstar = 5
            end 
        end

        if item_info.icon ~= nil then 
            icon.visible = true
            icon.url = "ui://ICONAtlas/" .. item_info.icon 
            if item_info.quality == nil or item_info.quality == 0 then
                item_info.quality = 1
            elseif item_info.quality > #Config.IconQuality then
                item_info.quality = #Config.IconQuality
            end
            quality.visible = true
            qualityBG.visible = true
            quality.url = "ui://ICONAtlas/" .. Config.IconFrame[item_info.quality]
            qualityBG.url = "ui://ICONAtlas/" .. Config.IconQuality[item_info.quality] 
        end
 
        if item_info.type ~= nil then
            occupation_icon.visible = true
            occupation_icon.url = "ui://ICONAtlas/" .. EnumConst.HeroTypeIcon[item_info.type]
        else
            occupation_icon.visible = false
        end 
    else
        icon.visible = false
        quality.visible = false
        qualityBG.visible = false
    end 
end

function UIAllocationResult.OnClickBack()
    DestoryUI(PanelNames.UIAllocationResult)
end

function UIAllocationResult.LocalListenEvent()
    -- ListenEvent(Config.EventType.Fresh_EquipUnWear,this.Unmount)
end

function UIAllocationResult.LocalCloseEvent()
    -- CloseEvent(Config.EventType.Fresh_EquipUnWear,this.Unmount)
end

function UIAllocationResult.OnDestroy() 
    this.removeEvent()
    this.hasInit = nil
    equipHandler.refreshHeroList = nil
    FireEvent(Config.EventType.ReFreshHeroPower)
end

function UIAllocationResult.OnEnable()
    this.LocalListenEvent()
end

function UIAllocationResult.OnDisable()
    this.LocalCloseEvent()
end 

return UIAllocationResult
